package com.my.common.result;

import lombok.*;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author: marshal
 * @date: 2021-10-15 3:43 PM
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1581330846770173770L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String message;


    @Getter
    @Setter
    private T data;

    public static <T> R<T> success() {
        return restResult(null, HttpStatusCodeEnum.SUCCESS.getCode(), HttpStatusCodeEnum.SUCCESS.getMessage());
    }

    public static <T> R<T> success(T data) {
        return restResult(data, HttpStatusCodeEnum.SUCCESS.getCode(), HttpStatusCodeEnum.SUCCESS.getMessage());
    }

    public static <T> R<T> success(T data, String message) {
        return restResult(data, HttpStatusCodeEnum.SUCCESS.getCode(), message);
    }

    public static <T> R<T> failed() {
        return restResult(null, HttpStatusCodeEnum.SYSTEM_ERROR.getCode(), HttpStatusCodeEnum.SYSTEM_ERROR.getMessage());
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, HttpStatusCodeEnum.SYSTEM_ERROR.getCode(), HttpStatusCodeEnum.SYSTEM_ERROR.getMessage());
    }

    public static <T> R<T> failed(String message) {
        return restResult(null, HttpStatusCodeEnum.SYSTEM_ERROR.getCode(), message);
    }

    public static <T> R<T> failed(T data, String message) {
        return restResult(data, HttpStatusCodeEnum.SYSTEM_ERROR.getCode(), message);
    }

    /**
     * 未登录
     *
     * @param data
     * @param <T>
     */
    public static <T> R<T> unauthorized(T data) {
        return restResult(data, HttpStatusCodeEnum.UNAUTHORIZED.getCode(), HttpStatusCodeEnum.UNAUTHORIZED.getMessage());
    }

    /**
     * 未授权
     *
     */
    public static <T> R<T> forbidden(T data) {
        return restResult(data, HttpStatusCodeEnum.FORBIDDEN.getCode(), HttpStatusCodeEnum.FORBIDDEN.getMessage());
    }

    private static <T> R<T> restResult(T data, int code, String message) {
        R<T> result = new R<>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        return result;
    }
}
